import {Component} from '@angular/core';
import {ActionSheetController, AlertController, IonicPage, NavController, NavParams} from 'ionic-angular';
import {Storage} from "@ionic/storage";
import {BusifoodTableorderPage} from "../busifood-tableorder/busifood-tableorder";
import {CashdetailPage} from "../business-food-cashdetail/business-food-cashdetail";
import {ChangeTablePage} from "../chang-table/change-table";
import {AppUrl} from "../../../app/app.general";
import {HttpClient} from "@angular/common/http";
import {BusinessFoodMinusPage} from "../business-food-minus/business-food-minus";

// import {CashdetailPage} from "../business-food/business-food-cashdetail/business-food-cashdetail";

/**
 * Generated class for the HotelManagePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-busifood-tableshow',
  templateUrl: 'busifood-tableshow.html',
})
export class BusifoodTableshowPage {
  private callBackData;
  tablename: string;
  tableid:number;
  // id: number;

  private tableSite:any = [];

  private currentTable;
  private selectFloor;
  private tableFloor;
  private sumCount;
  private status;

  private TableData = [];
  private GoodData = [];
  private restaurantid:number;

  private options = {
    viewDetail:
      {
        text: '查看详情',
        handler: () => {
          let data:any={
            tablename:this.tablename, orderno:null,tableid:this.tableid,sumPrice:0.00
          }
          this.navCtrl.push('CashdetailPage', {floor: this.tableFloor,data:data,pagetitle:'点餐明细'});
        }
      },
    used:
      {
        text: '开台',
        handler: () => {
          this.navCtrl.push('BusifoodTableorderPage', {
            callback: this.beginOrder,
            tableNo: this.tablename,
            tableid:this.tableid,
            floor: this.tableFloor,
            flag:'open',
            status: this.options.used,
          });
        }
      },
    checkCount:
      {
        text: '收银结账',
        handler: () => {
          let data:any={
            tablename:this.tablename, orderno:null,tableid:this.tableid,sumPrice:0.00
          }
          this.navCtrl.push('CashdetailPage', {floor: this.tableFloor,data:data, pagetitle:'收银',callback:this.checkcount});
        }
      },
    feeAdd:
      {
        text: '代理商品购买',
        handler: () => {
          this.navCtrl.push('MerBuyAgentStoreListPage');
        }
      },
    cleaned:
      {
        text: '收银完毕',
        handler: () => {
          this.currentTable.tableSite = this.tableSite[4];
          this.http.post(AppUrl.food.changeTableStatus,{id:this.tableid,status:0}).subscribe(data=>{
            console.log(data);
          });
        }
      },
    cancel:
      {
        text: '取消',
        role: 'cancel',
        handler: () => {
        }
      },
    stopToUse:
      {
        text: '暂停使用',
        handler: () => {
          this.currentTable.tableSite = this.tableSite[3];
          this.http.post(AppUrl.food.changeTableStatus,{id:this.tableid,status:4}).subscribe(data=>{
            console.log(data);
          });
        }
      },
    recoveryToUser:
      {
        text: '恢复使用',
        handler: () => {
          this.currentTable.tableSite = this.tableSite[4];
          this.http.post(AppUrl.food.changeTableStatus,{id:this.tableid,status:0}).subscribe(data=>{
            console.log(data);
          });
        }
      },
    cancelOrder:
      {
        text: '取消预订',
        handler: () => {
          this.showAlertCancelOrder();
        }
      },
    orderadd:
      {
        text: '加菜',
        handler: () => {
          // this.status = this.options.orderadd;
          this.navCtrl.push('BusifoodTableorderPage', {
            callback: this.beginOrder,
            tableNo: this.tablename,
            floor: this.tableFloor,
            tableid:this.tableid,
            flag:'add',
            GoodData: this.GoodData,
            status: this.options.orderadd
          });
          console.info('打一个加菜数据' + this.tablename + '"' + this.tableFloor + '[this.GoodData' + this.GoodData +this.status+ ']');
        }
      },
    orderMinus:
      {
        "text":'退菜',
        handler: () => {
          let data:any={
            tablename:this.tablename, orderno:null,tableid:this.tableid,sumPrice:0.00
          }
          this.navCtrl.push('BusinessFoodMinusPage',
            {floor: this.tableFloor,data:data,pagetitle:'退菜'});
        }
      }
  };

  constructor(public navCtrl: NavController, public navParams: NavParams, private alertCtrl: AlertController, private http:HttpClient,
              private actionSheetCtrl: ActionSheetController, private storage: Storage) {
    this.tablename = this.navParams.get('tablename');
    this.tableSite=[
      {name: '已开台', color: '#389cf4'},
      {name: '已收银', color: 'brown'},
      {name: '已预订', color: 'coral'},
      {name: '暂停使用', color: 'black'},
      {name: '空闲', color: '#41b883'},
      {name: '取消', color: '#41b883'}
    ];
    this.TableData=[];
    this.http.post(AppUrl.table.showtable,{userid:localStorage.getItem('userid')}).subscribe(data=>{
      let temp1:any=data;
      let temp:any[]=temp1.data;

      for(let i=0;i<temp.length;i++){
        let index=this.TableData.findIndex(e=>e.floorid==temp[i].floorid);
        let colorIndex=this.tableSite.findIndex(e=>e.name==temp[i].status);
        console.log(index);
        if(index!=-1){
          this.TableData[index].tables.push({
            id: temp[i].id,
            tablename: temp[i].tableno,
            tablesize: temp[i].tablesize,
            tableSite: {
              name:temp[i].status,
              color: this.tableSite[colorIndex].color
            },
          });
        }else{
          this.TableData.push({
            floorid:temp[i].floorid,
            floor:temp[i].floor,
            show:true,
            tables:[{
              id: temp[i].id,
              tablename: temp[i].tableno,
              tablesize: temp[i].tablesize,
              tableSite: {
                name:temp[i].status,
                color:this.tableSite[colorIndex].color
              },
            }]
          });
        }
      }
    });
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad BusifoodTableshowPage');
  }

  // ionViewWillEnter() {
  //   this.storage.get('TableData').then(data => {
  //     this.TableData = data;
  //   });
  //   this.storage.get('GoodData').then(data => {
  //     this.GoodData = data;
  //     // console.log(data);
  //   });
  // }

  showOptions(table, floor) {
    this.currentTable = table;
    this.tableFloor = floor;
    this.tablename = table.tablename;
    this.tableid=table.id;
    this.presentActionSheet(this.handleOptions());
  }

  handleOptions(): any {
    let buttons = [];
    switch (this.currentTable.tableSite.name) {
      //空闲状态
      case this.tableSite[4].name:
        buttons.push(this.options.used);
        // buttons.push(this.options.roomOrder);
        buttons.push(this.options.stopToUse);
        break;
      //已开台
      case this.tableSite[0].name:
        // buttons.push(this.options.feeAdd);
        buttons.push(this.options.viewDetail);
        buttons.push(this.options.checkCount);
        buttons.push(this.options.orderadd);
        buttons.push(this.options.orderMinus);
        break;
      //已收银
      case this.tableSite[1].name:
        buttons.push(this.options.cleaned);
        buttons.push(this.options.stopToUse);
        break;
      //已预订
      case this.tableSite[2].name:
        buttons.push(this.options.used);
        buttons.push(this.options.viewDetail);
        buttons.push(this.options.cancelOrder);
        break;
      //即将到期
      case this.tableSite[5].name:
        buttons.push(this.options.checkCount);
        // buttons.push(this.options.feeAdd);
        buttons.push(this.options.viewDetail);
        break;
      //暂停使用
      case this.tableSite[3].name:
        buttons.push(this.options.recoveryToUser);
        break;

    }
    buttons.push(this.options.cancel);
    return buttons;
  }

  showOrders() {
    this.navCtrl.push('OrdersNotificationPage', {callBack: this.getData});
  }

  changeState(item) {
    item.show = !item.show;
  }

  presentActionSheet(buttons) {
    let actionSheet = this.actionSheetCtrl.create({
      // title: '操作菜单',
      buttons: buttons
    });
    actionSheet.present();
  }


  getData = (data) => {
    return new Promise((resolve, reject) => {
      this.callBackData = data;
      console.log(this.callBackData);
      resolve();
    });
  };

  //开台点菜回调函数
  beginOrder = (data) => {
    return new Promise((resolve, reject)=>{
      console.log(data);
      this.currentTable.tableSite = this.tableSite[0];
      resolve();
    });
  };

  // 变更房间回调
  changeRoom = (changeRoomInfo) => {
    return new Promise((resolve, reject) => {
      this.currentTable.tableSite = this.tableSite.Idle;
      this.currentTable.cus = '';
      this.currentTable.leave = '';

      let fI = changeRoomInfo.floorIndex;
      let rI = changeRoomInfo.roomIndex;
      this.TableData[fI].tables[rI].tableSite = this.tableSite.used;
      this.TableData[fI].tables[rI].cus = changeRoomInfo.roomCustomer.val;
      this.TableData[fI].tables[rI].leave = changeRoomInfo.endTime.val;
      resolve();
    });
  };

  checkcount=(data)=>{
    return new Promise((resolve,reject)=>{
      this.currentTable.tableSite = this.tableSite[1];
      resolve();
    })
  };

  //取消预订
  showAlertCancelOrder() {
    let alert = this.alertCtrl.create();
    alert.setTitle('取消预订');
    alert.setMessage('确认要取消预定吗？')

    alert.addButton('放弃');
    alert.addButton({
      text: '确认',
      handler: data => {
        this.currentTable.tableSite = this.tableSite.Idle;
        this.currentTable.cus = '';
        this.currentTable.leave = '';
      }
    });
    alert.present();
  }


  // setStorage() {
  //   console.info("相关数据已初始化存储到本地");
  //   let TableData = [
  //     {
  //       floorid: '1', show: true, tables: [
  //         {
  //           id: 1,
  //           tablename: "一号桌",
  //           tableplace: "大厅",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.used,
  //         }, {
  //           id: 2,
  //           tablename: "二号桌",
  //           tableplace: "大厅",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.used,
  //         }, {
  //           id: 3,
  //           tablename: "三号桌",
  //           tableplace: "大厅",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.used,
  //         }, {
  //           id: 4,
  //           tablename: "四号桌",
  //           tableplace: "大厅",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }, {
  //           id: 5,
  //           tablename: "五号桌",
  //           tableplace: "大厅",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }, {
  //           id: 6,
  //           tablename: "六号桌",
  //           tableplace: "大厅",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }, {
  //           id: 7,
  //           tablename: "七号桌",
  //           tableplace: "大厅",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }],
  //     },
  //     {
  //       floorid: '2', show: false, tables: [
  //         {
  //           id: 8,
  //           tablename: "八号桌",
  //           tableplace: "大厅",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }, {
  //           id: 9,
  //           tablename: "九号桌",
  //           tableplace: "大厅",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }, {
  //           id: 10,
  //           tablename: "十号桌",
  //           tableplace: "大厅",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }, {
  //           id: 11,
  //           tablename: "富贵",
  //           tableplace: "包厢",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }, {
  //           id: 12,
  //           tablename: "鸿福",
  //           tableplace: "包厢",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }, {
  //           id: 13,
  //           tablename: "团圆",
  //           tableplace: "包厢",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }, {
  //           id: 14,
  //           tablename: "美满",
  //           tableplace: "包厢",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }],
  //     },
  //     {
  //       floorid: '3', show: false, tables: [
  //         {
  //           id: 15,
  //           tablename: "花好月圆",
  //           tableplace: "包厢",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }, {
  //           id: 16,
  //           tablename: "和合美满",
  //           tableplace: "包厢",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }, {
  //           id: 17,
  //           tablename: "风花雪月",
  //           tableplace: "包厢",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }, {
  //           id: 18,
  //           tablename: "湖光山色",
  //           tableplace: "包厢",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }, {
  //           id: 19,
  //           tablename: "山明水秀",
  //           tableplace: "包厢",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }, {
  //           id: 20,
  //           tablename: "小桥流水",
  //           tableplace: "包厢",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }, {
  //           id: 21,
  //           tablename: "彩霞缤纷",
  //           tableplace: "包厢",
  //           tablesize: "10人大桌",
  //           tableSite: this.tableSite.Idle,
  //         }]
  //     }
  //   ];
  //   let GoodData = [
  //     {
  //       floor: 1,
  //       orderTable: [
  //         {
  //           id: 1,
  //           tableNo: "一号桌",
  //           tableSite: this.tableSite.used,
  //           goodsList: [
  //             {goodname: "乡村牛肉", goodprice: 20.00, sumBuy: 1, sumPrice: 20.00, isbuy: true},
  //             {goodname: "小炒肉", goodprice: 15.00, sumBuy: 2, sumPrice: 30.00, isbuy: true},
  //             {goodname: "水煮肉片", goodprice: 15.00, sumBuy: 2, sumPrice: 30.00, isbuy: true},
  //             {goodname: "灯影牛肉", goodprice: 28.00, sumBuy: 2, sumPrice: 56.00, isbuy: true}
  //           ], sumCount: 136.00
  //         },
  //         {
  //           id: 2,
  //           tableNo: "二号桌",
  //           tableSite: this.tableSite.used,
  //           goodsList: [
  //             {goodname: "土坛烤肉", goodprice: 10.00, sumBuy: 1, sumPrice: 10.00, isbuy: true},
  //             {goodname: "麻辣牛肉", goodprice: 20.00, sumBuy: 2, sumPrice: 40.00, isbuy: true},
  //             {goodname: "柠檬鸡", goodprice: 15.00, sumBuy: 2, sumPrice: 30.00, isbuy: true},
  //             {goodname: "麻辣牛肉", goodprice: 15.00, sumBuy: 2, sumPrice: 30.00, isbuy: true}
  //           ], sumCount: 110.00
  //         },
  //         {
  //           id: 3,
  //           tableNo: "三号桌",
  //           tableSite: this.tableSite.used,
  //           goodsList: [
  //             {goodname: "豆花饭", goodprice: 10.00, sumBuy: 1, sumPrice: 10.00, isbuy: true},
  //             {goodname: "凉卷粉", goodprice: 5.00, sumBuy: 2, sumPrice: 10.00, isbuy: true},
  //             {goodname: "木瓜水", goodprice: 3.00, sumBuy: 3, sumPrice: 9.00, isbuy: true},
  //             {goodname: "龟苓膏", goodprice: 4.00, sumBuy: 4, sumPrice: 16.00, isbuy: true}
  //           ], sumCount: 45.00
  //         }
  //       ]
  //     },
  //     {
  //       floor: 2,
  //       orderTable: [
  //         {
  //           id: 21,
  //           tableNo: "二十一号桌",
  //           tableSite: this.tableSite.used,
  //           goodsList: [
  //             {goodname: "豆花饭", goodprice: 10.00, sumBuy: 1, sumPrice: 10.00, isbuy: true},
  //             {goodname: "凉卷粉", goodprice: 5.00, sumBuy: 2, sumPrice: 10.00, isbuy: true},
  //             {goodname: "木瓜水", goodprice: 3.00, sumBuy: 3, sumPrice: 9.00, isbuy: true},
  //             {goodname: "龟苓膏", goodprice: 4.00, sumBuy: 4, sumPrice: 16.00, isbuy: true}
  //           ], sumCount: 45.00
  //         },
  //         {
  //           id: 22,
  //           tableNo: "二十二号桌",
  //           tableSite: this.tableSite.used,
  //           goodsList: [
  //             {goodname: "土坛烤肉", goodprice: 10.00, sumBuy: 1, sumPrice: 10.00, isbuy: true},
  //             {goodname: "麻辣牛肉", goodprice: 20.00, sumBuy: 2, sumPrice: 40.00, isbuy: true},
  //             {goodname: "柠檬鸡", goodprice: 15.00, sumBuy: 2, sumPrice: 30.00, isbuy: true},
  //             {goodname: "麻辣牛肉", goodprice: 15.00, sumBuy: 2, sumPrice: 30.00, isbuy: true}
  //           ], sumCount: 110.00
  //         },
  //         {
  //           id: 23,
  //           tableNo: "二十三号桌",
  //           tableSite: this.tableSite.used,
  //           goodsList: [
  //             {goodname: "土坛烤肉", goodprice: 10.00, sumBuy: 1, sumPrice: 10.00, isbuy: true},
  //             {goodname: "麻辣牛肉", goodprice: 20.00, sumBuy: 2, sumPrice: 40.00, isbuy: true},
  //             {goodname: "柠檬鸡", goodprice: 15.00, sumBuy: 2, sumPrice: 30.00, isbuy: true},
  //             {goodname: "麻辣牛肉", goodprice: 15.00, sumBuy: 2, sumPrice: 30.00, isbuy: true}
  //           ], sumCount: 110.00
  //         }
  //       ]
  //     },
  //     {
  //       floor: 3,
  //       orderTable: [
  //         {
  //           id: 31,
  //           tableNo: "三十一号桌",
  //           tableSite: this.tableSite.used,
  //           goodsList: [
  //             {goodname: "豆花饭", goodprice: 10.00, sumBuy: 1, sumPrice: 10.00, isbuy: true},
  //             {goodname: "凉卷粉", goodprice: 5.00, sumBuy: 2, sumPrice: 10.00, isbuy: true},
  //             {goodname: "木瓜水", goodprice: 3.00, sumBuy: 3, sumPrice: 9.00, isbuy: true},
  //             {goodname: "龟苓膏", goodprice: 4.00, sumBuy: 4, sumPrice: 16.00, isbuy: true}
  //           ], sumCount: 45.00
  //         }
  //       ]
  //     },
  //   ]
  //
  //   this.storage.set('TableData', TableData);
  //   this.storage.set('GoodData', GoodData);
  // }
  //
  // saveTableDataStorage() {
  //   this.storage.set('TableData', this.TableData);
  //   this.storage.set('GoodData', this.GoodData);
  // }
}
